package com.zpphis.controller;

import com.zpphis.entity.Department;
import com.zpphis.entity.DepartmentType;
import com.zpphis.service.IDepartmentService;
import com.zpphis.service.IDepartmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    private IDepartmentTypeService departmentTypeService;
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/table.json")
    public Map<String, Object> table() {

        return new HashMap<String, Object>() {
            {
                put("code", 0);
                put("data", departmentService.listExtend());
            }
        };
    }

    @RequestMapping("/add.json")
    public Map<String, Object> add(Department entity) {
        if (departmentService.save(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "新增失败！");
                }
            };
        }
    }

    @RequestMapping("/del.json")
    public Map<String, Object> del(int id) {
        if (departmentService.removeById(id)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "删除失败！");
                }
            };
        }
    }

    @RequestMapping("/queryOne.json")
    public Department queryOne(int id) {
        return departmentService.getById(id);
    }

    @RequestMapping("/edit.json")
    public Map<String, Object> edit(Department entity) {
        if (departmentService.updateById(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "修改失败！");
                }
            };
        }
    }

    @RequestMapping("/typeTable.json")
    public Map<String, Object> typeTable() {

        return new HashMap<String, Object>() {
            {
                put("code", 0);
                put("data", departmentTypeService.list());
            }
        };
    }

    @RequestMapping("/typeAdd.json")
    public Map<String, Object> typeAdd(DepartmentType entity) {
        if (departmentTypeService.save(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "新增失败！");
                }
            };
        }
    }

    @RequestMapping("/typeDel.json")
    public Map<String, Object> typeDel(int id) {
        if (departmentTypeService.removeById(id)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "删除失败！");
                }
            };
        }
    }

    @RequestMapping("/typeQueryOne.json")
    public DepartmentType typeQueryOne(int id) {
        return departmentTypeService.getById(id);
    }

    @RequestMapping("/typeEdit.json")
    public Map<String, Object> typeEdit(DepartmentType entity) {
        if (departmentTypeService.updateById(entity)) {
            return new HashMap<String, Object>() {
                {
                    put("code", 1);
                }
            };
        } else {
            return new HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "修改失败！");
                }
            };
        }
    }
}
